package easier.app.x.result.pic;

import android.Manifest.permission;
import android.graphics.Bitmap;
import android.net.Uri;
import androidx.activity.result.ActivityResultCallback;
import androidx.fragment.app.FragmentManager;
import easier.app.x.result.Permission;
import easier.app.x.result.ResultFragmentFactory;
import java.util.List;

/**
 * 拍照
 */
public class TakePhoto {

    /**
     * @param manager  拍照
     * @param callback 回调
     */
    public static void take( FragmentManager manager, Uri fileUri, ActivityResultCallback< Uri > callback ) {

        Permission.registerForPermissionResult( manager, permission.CAMERA, new ActivityResultCallback< Boolean >() {
            @Override
            public void onActivityResult( Boolean result ) {

                if ( result ) {
                    manager
                        .beginTransaction()
                        .add( ResultFragmentFactory.buildTakePhoto( fileUri, callback ), "take-pic-fragment" )
                        .commit();
                } else {
                    if ( callback != null ) {
                        callback.onActivityResult( null );
                    }
                }
            }
        } );
    }

    /**
     * @param manager  获取预览图,自己处理保存
     * @param callback 回调
     */
    public static void takePreview( FragmentManager manager, ActivityResultCallback< Bitmap > callback ) {

        Permission.registerForPermissionResult( manager, permission.CAMERA, new ActivityResultCallback< Boolean >() {
            @Override
            public void onActivityResult( Boolean result ) {

                if ( result ) {
                    manager
                        .beginTransaction()
                        .add( ResultFragmentFactory.buildTakePhotoPreview( callback ), "take-pic-preview-fragment" )
                        .commit();
                } else {
                    if ( callback != null ) {
                        callback.onActivityResult( null );
                    }
                }
            }
        } );
    }

    /**
     * @param manager  从相册选择
     * @param callback 回调
     */
    public static void fromGallery( FragmentManager manager, ActivityResultCallback< Uri > callback ) {

        Permission.registerForPermissionResult( manager, permission.READ_EXTERNAL_STORAGE,
            new ActivityResultCallback< Boolean >() {
                @Override
                public void onActivityResult( Boolean result ) {

                    if ( result ) {
                        manager
                            .beginTransaction()
                            .add( ResultFragmentFactory.buildGetContentPicture( callback ), "get_content-pic-fragment" )
                            .commit();
                    } else {
                        if ( callback != null ) {
                            callback.onActivityResult( null );
                        }
                    }
                }
            } );
    }

    /**
     * @param manager  从相册选择
     * @param callback 回调
     */
    public static void fromGalleryMultiple( FragmentManager manager, ActivityResultCallback< List< Uri > > callback ) {

        Permission.registerForPermissionResult( manager, permission.READ_EXTERNAL_STORAGE,
            new ActivityResultCallback< Boolean >() {
                @Override
                public void onActivityResult( Boolean result ) {

                    if ( result ) {
                        manager
                            .beginTransaction()
                            .add( ResultFragmentFactory.buildMultipleGetContentPicture( callback ), "get-multiple-content-pic"
                                + "-fragment" )
                            .commit();
                    } else {
                        if ( callback != null ) {
                            callback.onActivityResult( null );
                        }
                    }
                }
            } );
    }
}
